package com.fauks.ssm.springmvc.annotation;

import com.fauks.ssm.springmvc.annotation.bean.Dog;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
/*该处理器的命名空间*/
@RequestMapping("/anno")
public class AnnoFirstController {

    //配置路径和限定请求方式
    @RequestMapping(value = {"/index", "/hehe"}, method = RequestMethod.POST)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "helloAnno");
        modelAndView.setViewName("anno");
        return modelAndView;
    }

    //uri使用通配符，路径要么以home结尾要么以haha开头
    @RequestMapping(value = {"/*home", "/haha*"})
    public ModelAndView home() {
        return new ModelAndView("anno");
    }

    //请求的uri必须携带age参数且不能有name参数，其他参数随意
    @RequestMapping(value = "/params", params = {"age", "!name"})
    public ModelAndView params() {
        ModelAndView modelAndView = new ModelAndView("anno");
        return modelAndView;
    }

    //请求的参数名可能为username,则方法接收不到使用RequestParam校正
    @RequestMapping(value = "/params2")
    public ModelAndView params2(@RequestParam("username") String name) {
        ModelAndView modelAndView = new ModelAndView("anno");
        modelAndView.addObject("message", name);
        return modelAndView;
    }

    //路径参数参数包含在路径中
    @RequestMapping(value = "/{username}/{age}/params3")
    public ModelAndView params3(@PathVariable("username") String name, @PathVariable("age") int age) {
        ModelAndView modelAndView = new ModelAndView("anno");
        modelAndView.addObject("message", name + "::" + age);
        return modelAndView;
    } //路径参数参数包含在路径中

    @RequestMapping("/params4")
    public ModelAndView params4(Dog dog) {
        ModelAndView modelAndView = new ModelAndView("anno");
        modelAndView.addObject("message", dog);
        System.out.println(dog);
        return modelAndView;
    }

    @RequestMapping("/map")
    @ResponseBody
    public Map<String, String> returnMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("username", "fauks");
        map.put("age", "12");
        return map;
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<String> returnList() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("t");
        return list;
    }

    @RequestMapping("/itemType")
    public String itemType(Model model) {
        Map<String, String> map = new HashMap<>();
        map.put("single", "12");
        map.put("mul", "2");
        model.addAttribute("itemPrice", map);
        return "anno";
    }

    @ModelAttribute("itemType")
    public Map<String, String> modelAttr() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "fauks");
        map.put("address", "qd");
        return map;
    }

}
